package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.FireSysData;
import com.yqg.protection.service.IFireSysDataService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * FireSysDataController 前端控制器
 * @author zxf
 * @since 2024-09-18
 */
@RestController
@RequestMapping("/fire-sys-data")
public class FireSysDataController {

    @Resource
    private IFireSysDataService fireSysDataService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody FireSysData fireSysData) {
        return Result.success(fireSysDataService.saveOrUpdate(fireSysData));
    }
    /**
    *  删除
    */
    @PostMapping("/delete")
    @SysLog("删除设备传输日志")
    public Result delete(@RequestBody FireSysData fireSysData) {
        LambdaQueryWrapper<FireSysData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FireSysData::getEquipmentId, fireSysData.getEquipmentId());
        queryWrapper.eq(FireSysData::getExplanation, fireSysData.getExplanation());
        queryWrapper.eq(FireSysData::getEnterTime, fireSysData.getEnterTime());
        return Result.success(fireSysDataService.remove(queryWrapper));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<FireSysData> queryWrapper = new QueryWrapper<>();
       List<FireSysData> list=  fireSysDataService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(fireSysDataService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                           String equipmentId,
                           String explanation,
                           String startTime,
                           String endTime) {
        QueryWrapper<FireSysData> queryWrapper = new QueryWrapper<>();
        if (startTime != null && !startTime.equals("")&&endTime != null && !endTime.equals("")) {
            queryWrapper.between("enter_time", startTime, endTime);
        }
        queryWrapper.eq("equipment_id", equipmentId);
        queryWrapper.like("explanation", explanation);
        queryWrapper.orderByDesc("enter_time");
        return Result.success(fireSysDataService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

